require "test/unit"
require "trees/heap.rb"
require "testing_utils.rb"

class TestHeap < Test::Unit::TestCase
  TOT_VALS = 128        # bad ruby: no local constants
  
  def test_max_insert()
    heap = Heap.new_max_heap()
    _test_heap(heap) {|val_0, val_1| val_0 >= val_1 }
  end
  
  def test_min_insert
    heap = Heap.new_min_heap()
    _test_heap(heap) {|val_0, val_1| val_0 <= val_1 }
  end
  
  def test_fail_max_top_up()
    heap = Heap.new_max_heap()
    
    heap.insert(0)
    
    assert_raise(RuntimeError) do
      heap.top_up_key(0, 0)           # 0->0: not topping up
    end
  end
  
  private

  # pass the heap and the order validation function
  def _test_heap(heap, &heap_compare)
    output = []
    input = rand_array(TOT_VALS, TOT_VALS)

    # insert the values in the heap and extract them into the actual array 
    
    input.each {|value| heap.insert(value) }

    TOT_VALS.times do
      value = heap.extract_top()
      output.push(value)
    end

    assert_ordered(output, heap_compare)
  end
end